﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using WXR2BlogML.Properties;

namespace WXR2BlogML
{
	public static class WxrToBlogMlConverter
	{
		public static ConversionResult Convert(string inputFile)
		{
			FileInfo source = new FileInfo(inputFile);
			if (!source.Exists)
			{
				return new ConversionResult { Errors = string.Format("File: {0} not found.", inputFile) };
			}
			string target = Path.Combine(source.DirectoryName,
												  Path.GetFileNameWithoutExtension(source.Name) + "_BlogML" +
												  Path.GetExtension(source.Name));

			// process the file and convert the values
			string destfile = inputFile.Replace(".xml", ".converted.xml");
			// XhtmlToXmlUtils.XhtmlToXml(file, destfile);
			WordpressExportFix.FixExportFile(inputFile, destfile);

			XElement channelWxr = XElement.Load(destfile).Element("channel");
			DateTime created = WxrBlogMlExporter.ParseWPDate(channelWxr.Element("pubDate").Value);

			XElement allCats = WxrBlogMlExporter.CreateCategories(channelWxr);
			XElement allAuthors = WxrBlogMlExporter.CreateAuthors(channelWxr);


			XElement blog = new XElement(WxrBlogMlExporter.blogMLNs + "blog",
				 new XAttribute("date-created", WxrBlogMlExporter.FormatWpDate(created)),
				 new XAttribute("root-url", "/"),
				 new XElement(WxrBlogMlExporter.blogMLNs + "title",
					  new XAttribute("type", "text"), new XCData(channelWxr.Element("title").Value)),
				 new XElement(WxrBlogMlExporter.blogMLNs + "sub-title",
					  new XAttribute("type", "text"), new XCData(channelWxr.Element("description").Value)),
				 allAuthors,
				 WxrBlogMlExporter.CreateExtendedProperties(),
				 allCats
				//#if BLOGML25
				//                 WxrBlogMlExporter.CreateTags(channelWxr), //only in blogml 2.5 I need 2.0 for blogengine
				//#endif
				// WxrBlogMlExporter.CreatePosts(channelWxr, allAuthors, allCats)
					  );
			if (BlogMlSettings.Blogmltype == BlogMlSettings.Blogml25)
				blog.Add(WxrBlogMlExporter.CreateTags(channelWxr));
			blog.Add(WxrBlogMlExporter.CreatePosts(channelWxr, allAuthors, allCats));

			XDocument blogMl = new XDocument(blog);
			blogMl.Save(target);

			Console.WriteLine(@"Saved file: " + target);

			StringBuilder sbErrors = new StringBuilder();

			XmlSchemaSet schema = new XmlSchemaSet();
			if (BlogMlSettings.Blogmltype == BlogMlSettings.Blogml25)

				schema.Add(WxrBlogMlExporter.blogMLNs.NamespaceName, XmlReader.Create(Resources.BlogML25));
			else
				schema.Add(WxrBlogMlExporter.blogMLNs.NamespaceName, XmlReader.Create(new StringReader(Resources.BlogML)));

			blogMl.Validate(schema, ((sender, e) => sbErrors.AppendLine(e.Message)));

			string errors = sbErrors.ToString();

			return new ConversionResult { ConvertedFile = target, Errors = errors };
		}
	}

	public class ConversionResult
	{
		/// <summary>
		/// the file that was generated for the conversion
		/// </summary>
		public string ConvertedFile { get; set; }

		/// <summary>
		/// a list of error in the converted file
		/// </summary>
		public string Errors { get; set; }

		public bool Succeded { get { return string.IsNullOrEmpty(Errors); } }
	}
}